package user.zc.util;

import user.zc.config.user.config.ConnectionConfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.function.Function;

/**
 * @author zhouchuang
 * @date 2023/8/8 16:55
 */
public class ConnectPool {
    public static Connection getConnection()throws Exception{
        return DriverManager.getConnection(ConfigGroupHelper.getInst().get(ConnectionConfig.class).getUrl(),
                ConfigGroupHelper.getInst().get(ConnectionConfig.class).getUser(),
                ConfigGroupHelper.getInst().get(ConnectionConfig.class).getPassword());
    }
    public static <R> R execute(Function<Connection,R> dataCollector)throws Exception{
        try(Connection conn = getConnection()){
            return dataCollector.apply(conn);
        }
    }
}
